﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.VisualBasic;

namespace TIMSComponents
{
    public partial class FrmFindM : Form
    {
        private DataAccessLayer DAL=new DataAccessLayer();

        private DataTable DataSource = new DataTable();
        private string FieldKeyNN;
        private string FieldKeyNS;
        private string SortFN;
        private int[] CollumeWidth;
        private string[] CollumsCaption;
        private string CondisionText;
        private string[] ReturnedField;
        internal StringDictionary Result;


        public FrmFindM(DataTable PDataSource, string PFieldKeyNN, string PFieldKeyNS, string[] PReturnedField, string PSortFN, int[] PCollumeWidth,string[] PcollumsCaption, string PCondisionText)
        {
            InitializeComponent();

            DataSource =PDataSource;
            FieldKeyNN=PFieldKeyNN;
            FieldKeyNS=PFieldKeyNS;
            ReturnedField = PReturnedField;
            SortFN= PSortFN;
            CollumeWidth=PCollumeWidth;
            CollumsCaption = PcollumsCaption;
            CondisionText=PCondisionText;            
        }

        
        private void Sort()
        {
            if (string.IsNullOrEmpty(SortFN)==false  && gridView1.Columns.Count > 0)
            {
                bindingSource1.Sort = SortFN + " asc"; // dataGridView1.Columns[SortFN], ListSortDirection.Ascending);                
            }
        }

        public void SetCollumeName()
        {
            //gridView1.Columns[i].Caption=""
            int MinItem;
            if (CollumsCaption != null)
            {
                //Get minimum size array 
                MinItem = Math.Min(CollumsCaption.Length, gridView1.Columns.Count);
                for (int i = 0; i < MinItem; i++)
                {
                    gridView1.Columns[i].Caption = CollumsCaption[i].ToString();
                }
            }
        }
        public void SetCollumeSize()
        {
            int MinItem;
            if (CollumeWidth != null)
            {
                //Get minimum size array 
                MinItem = Math.Min(CollumeWidth.Length, gridView1.Columns.Count);
                for(int i=0; i<MinItem-1; i++)
                {
                    gridView1.Columns[i].Width = CollumeWidth[i];
                    
                }
            }
        }

        private void FrmFindM_Load(object sender, EventArgs e)  
        {
            bindingSource1.DataSource = DataSource;
            gridControl1.DataSource = bindingSource1;
            if (textBoxExpr.Text == CondisionText.Trim())
            {
                textBoxExpr_TextChanged(sender, e);
            }
            else
            {
                textBoxExpr.Text = CondisionText.Trim();
            }
            
            textBoxExpr.Focus();
            //Call procedure to correcting grid collume size
            SetCollumeSize();
            SetCollumeName();
            Sort();
        }

        
        private bool IsSeparateChar;
        private void textBoxExpr_TextChanged(object sender, EventArgs e)
        {

            if (IsSeparateChar==false && string.IsNullOrEmpty(textBoxExpr.Text.Trim()) ==false)
            {
                bindingSource1.DataSource = DAL.GetFilteredData(DataSource, CMain.GetWhereCondition(FieldKeyNN, FieldKeyNS, textBoxExpr.Text));
            }
            else if (string.IsNullOrEmpty(textBoxExpr.Text))
            {
                bindingSource1.DataSource = DataSource;
            }            
            IsSeparateChar = false;
        }

        private void textBoxExpr_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == '.' || e.KeyChar == '*' || e.KeyChar == '%')
            {
                IsSeparateChar = true;
            }
            
        }



        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                buttonOk_Click(sender, e);
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (ReturnedField != null)
            {
                Result = new StringDictionary();
                int MinItem = Math.Min(ReturnedField.Length, gridView1.Columns.Count);
                for (int i = 0; i < MinItem; i++)
                {
                    Result.Add(ReturnedField[i],gridView1.GetFocusedRowCellDisplayText(ReturnedField[i].ToString() ));
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("ReturnedField's value is null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCnacel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();  
        }

        private void textBoxExpr_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Down && gridView1.RowCount > 0)
            {
                gridView1.Focus();
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {

            if(e.KeyCode==Keys.Up && gridView1.FocusedRowHandle == 0)
            {
                textBoxExpr.Focus();
            }
        }

        private void gridControl1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (char.IsControl(e.KeyChar) == false)
            {

                textBoxExpr.Focus();
                textBoxExpr.Text += e.KeyChar;
            }
        }

    }
}

